package com.junci.mtest.vm;

import static com.junci.mtest.model.Constants.PTEST;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.junci.mtest.model.bean.ErrorMessage;
import com.junci.mtest.model.bean.TestPlanBean;
import com.junci.mtest.model.repository.HttpDefaultCallback;
import com.junci.mtest.model.repository.HttpDefaultRepository;

import java.util.List;

public class PlanViewModel extends ViewModel {

    private final MutableLiveData<List<TestPlanBean.Data.Rows>> itemsLiveData = new MutableLiveData<>();
    private final MutableLiveData<TestPlanBean.Data.Rows> itemData = new MutableLiveData<>();
    public LiveData<TestPlanBean.Data.Rows> getItem() {
        return itemData;
    }
    public LiveData<List<TestPlanBean.Data.Rows>> getItems() {
        return itemsLiveData;
    }
    public void setItemData(TestPlanBean.Data.Rows data){
        itemData.setValue(data);
    }
    //刷新事件
    public void refresh() {

        new HttpDefaultRepository<TestPlanBean>().post(PTEST, null, TestPlanBean.class, true, new HttpDefaultCallback<TestPlanBean>() {
            @Override
            protected void success(TestPlanBean bean) {

                itemsLiveData.postValue(bean.getData().getRows());

            }
            @Override
            protected void failed(ErrorMessage errorMessage) {

            }
        },true);
    }


    public void fetchItems() {
        //请求数据
        new HttpDefaultRepository<TestPlanBean>().post(PTEST, null, TestPlanBean.class, true, new HttpDefaultCallback<TestPlanBean>() {
            @Override
            protected void success(TestPlanBean bean) {
                itemsLiveData.postValue(bean.getData().getRows());
            }
            @Override
            protected void failed(ErrorMessage errorMessage) {

            }
        },true);
    }
}